
package com.intretech.umsin.system.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.intretech.umsin.common.entity.DictModel;
import com.intretech.umsin.system.entity.SysDictItemEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
* @version V1.0
* 系统字典详情信息:sys_dict_item 对应数据访问接口.
* @InterfaceName: SysDictItemMapper
* @Description: 系统字典详情信息:sys_dict_item 对应数据访问接口.
* @author: 李启联
* @date:   2022-8-6 14:37:22
* @Copyright: Copyright (c) 2022
*/

@Mapper
public interface SysDictItemMapper extends BaseMapper<SysDictItemEntity> {
    /**
     * 根据字典类型编码查出对应的所有字典条目的数据集合
     * 注意：
     * 1，@Select里面的内容需要使用{}括起来，使用<script>标签将sql语句包裹
     * 2，<script>中的sql代码，每行后面使用逗号结束，不要使用+号
     * @param codes
     * @return
     */
    @Select({"<script>",
            " select ",
            " b.dict_code as `dictCode`, ",
            " a.item_value as `value`," ,
            " a.item_text as `text`",
            " from sys_dict_item a inner join sys_dict b on a.dict_id=b.id ",
            " where b.dict_code in ",
            "<foreach item='item' index='index' collection='codes' open='(' separator=',' close=')'>",
            "#{item}",
            "</foreach>",
            "</script>"})
    List<DictModel> queryManyDictByDictCodes(@Param("codes") List<String> codes);



    /**
     * 通过查询指定table的 text code key 获取字典值
     * 查询字典表的数据 支持设置过滤条件、设置存储值作为in查询条件
     * @param table 表名称 （或表名称 + " where k = 'v'"）
     * @param text 名称字段
     * @param code 码字段
     * @param filterSql 附加条件
     * @param codeValues 码名映射中“码”的值集合 支持批量查询
     * @return
     */
    @Select({"<script>",
            " select ",
            " ${text} as `text`,",
            " ${code} as `value`",
            " from ${table}",
            " where 1=1 " ,
            "<if test=\"codeValues.size()>0\">",
            " and ${code} in ",
            "<foreach item='key' index='index' collection='codeValues' open='(' separator=',' close=')'>",
            "#{key}",
            "</foreach>",
            "</if>",
            "<if test=\"filterSql != null and filterSql != ''\">",
            " and ${filterSql}",
            "</if>",
            "</script>"})
    List<DictModel> queryTableDictByKeysAndFilterSql(@Param("table") String table, @Param("text") String text, @Param("code") String code, @Param("filterSql") String filterSql, @Param("codeValues") List<String> codeValues);



}

